<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head profile="http://gmpg.org/xfn/11">
<title>Jar Class Loader</title>
<link rel="shortcut icon" href="image/favicon.ico" />
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
<meta name="verify-v1" content="8VK6C5sBZ2AxprAkCEvDSxPlj6tKGe75Te12yxe426w=" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en-gb" />
<meta http-equiv="imagetoolbar" content="false" />
<meta name="author" content="Xeus Technologies" />
<meta name="description" content="Jar Class Loader" />
<meta name="keywords" content="jar class loader, jarclassloader, classloader, java, jar, Kamran, Kamran Zafar, Xeus, Xeus Technologies, spring, spring framework, spring classloader, spring class loader, jar class loader, jarclassloader, classloader, java, jar, Kamran, Kamran Zafar, Xeus, Xeus Technologies, spring, spring framework, spring classloader, spring class loader" />
<meta name="mssmarttagspreventparsing" content="true" />
<meta name="robots" content="index, follow, noarchive" />
<meta name="revisit-after" content="7 days" />
<link type="text/css" rel="stylesheet" href="css/SyntaxHighlighter.css" />
<script language="javascript" src="js/shCore.js"></script>
<script language="javascript" src="js/shBrushJava.js"></script>
<script language="javascript" src="js/shBrushXml.js"></script>
<script language="javascript">
	window.onload = function () {
		dp.SyntaxHighlighter.ClipboardSwf = 'js/clipboard.swf';
		dp.SyntaxHighlighter.HighlightAll('code');
	}
	</script>
</head>

<body>

<div id="header">
  <h1>JCL</h1>
  <h2>jar class loader</h2>
</div>
<div id="navigation">
  <ul>
    <li><a href="#" class="active">Home</a></li>
    <li><a href="http://www.sourceforge.net/projects/jcloader">Download</a></li>
    <li><a href="http://kamranzafar.co.cc/">Author</a></li>
    <li><a href="http://xeustech.blogspot.com/">Projects</a></li>
    <li><a href="http://xeustechnologies.org/">Company</a></li>
    <li><a href="mailto:xeus.man@gmail.com">Contact</a></li>
  </ul>
</div>
<div id="content">
  <h1>Overview</h1>
  <p>JCL is a simple Java API that allows loading classes from Jar files. It uses 
  a custom class loader to load classes from JARs, which helps in isolating class 
  loaders and it even makes it possible to load multiple versions of the same class 
  file from different jars. JCL also integrates with Spring; the integration is 
  tested with Spring 2.5.</p>
  <h1>Installation</h1>
  <p>To use JCL, <a href="http://www.sourceforge.net/projects/jcloader">download</a> 
  and build the JCL project and put jcl.jar and dependencies in the application&#39;s 
  classpath.</p>
  <h1>Usage</h1>
  <p>JCL is a light weight API and has only a few but useful classes. Here is a 
  simple example on how to programmatically use JCL. JarClassLoader has an arguments-constructor and <b><i>add</i></b> methods that 
  take jar-file/class-folder paths, URLs and InputStreams.</p>
  <p></p>
  <pre name="code" class="java">			JarClassLoader jcl = new JarClassLoader();
			jcl.add(&quot;myjar.jar&quot;); //Load jar file
			jcl.add(new URL(&quot;http://myserver.com/myjar.jar&quot;)); //Load jar from a URL
			jcl.add(new FileInputStream(&quot;myotherjar.jar&quot;)); //Load jar file from stream
			jcl.add(&quot;myclassfolder/&quot;); //Load class folder
			jcl.add(&quot;myjarlib/&quot;); //Recursively load all jar files in the folder/sub-folder(s)
			
			JclObjectFactory factory = JclObjectFactory.getInstance();
			
			//Create object of loaded class
			Object obj = factory.create(jcl,&quot;mypackage.MyClass&quot;);
			</pre>

  <p>Now in order to invoke the methods of the object one has to use reflection.</p>
  <p>Similarly JCL can be used with spring framework. </p>
  <pre name="code" class="xml">			
&lt;!-- Singleton object factory --&gt;
&lt;bean id=&quot;jclFactory&quot; class=&quot;xeus.jcl.JclObjectFactory&quot;&gt;&lt;/bean&gt;
&lt;!-- Test jar class loader --&gt;
&lt;bean id=&quot;jcl&quot; class=&quot;xeus.jcl.JarClassLoader&quot;&gt;
	&lt;constructor-arg&gt;
		&lt;list&gt;
			&lt;value&gt;myjar.jar&lt;/value&gt;
			&lt;ref bean=&quot;someJarInputStream&quot;&gt;&lt;/ref&gt;
			&lt;ref bean=&quot;someJarURL&quot; /&gt;&lt;ref&gt;&lt;/ref&gt;
		&lt;/list&gt;
	&lt;/constructor-arg&gt;
&lt;/bean&gt;

&lt;!-- 
	The Bean arguments: 
	1- The jar class loader
	2- Name of the class to be loaded
	The rest are optional arguments used in constructor/setter injection for the bean to be loaded 
--&gt;
&lt;bean id=&quot;test&quot; factory-bean=&quot;jclFactory&quot; factory-method=&quot;create&quot;&gt;
	&lt;!-- Factory-method arguments --&gt;
	&lt;constructor-arg&gt;
		&lt;ref bean=&quot;jcl&quot;&gt;&lt;/ref&gt;
	&lt;/constructor-arg&gt;
	&lt;constructor-arg&gt;
		&lt;value&gt;mypackage.MyClass&lt;/value&gt;
	&lt;/constructor-arg&gt;

	&lt;!-- Bean arguments --&gt;
	&lt;constructor-arg&gt;
		&lt;value&gt;Hello&lt;/value&gt;
	&lt;/constructor-arg&gt;
	&lt;property name=&quot;name&quot;&gt;
		&lt;value&gt;Kamran Zafar&lt;/value&gt;
	&lt;/property&gt;
&lt;/bean&gt;
			</pre>
<br/>
  <h3>Casting objects loaded in JCL</h3>
<p> Java disallows casting objects loaded in a different classloader; such casting attempt results in a ClassCastException. But sometimes it is necessary to cast objects to obtain interface references
loaded in the current classloader. E.g. suppose that an API implementation is loaded using JCL and the API itself is loaded in the current classloader, now it is easy to use the interface reference to invoke methods than using reflection. JCL provides a few ways to obtain interface references and coverting the JCL-loaded objects into castable objects.</p>
  <pre name="code" class="java">			JarClassLoader jcl = new JarClassLoader();
			jcl.add(&quot;myapi-impl.jar&quot;); //Load jar file
			
			JclObjectFactory factory = JclObjectFactory.getInstance();
			
			//Create object of loaded class
			Object obj = factory.create(jcl,&quot;myapi.impl.MyInterfaceImpl&quot;);

			//Obtain interface reference in the current classloader
			MyInterface mi = JclUtils.cast(obj, MyInterface.class);

			//Convert the object into a castable object in the current classloader
			MyInterface mi1 = (MyInterface) JclUtils.toCastable(obj, MyInterface.class);

			//Clone "Serializable" object into a castable object in the current classloader
			//The JCL-loaded object must implement Serializable
			MyInterface mi2 = (MyInterface) JclUtils.clone(obj);
			</pre>
<br />
<h3>Manipulating class loading order & adding custom classloaders</h3>
  <p>JCL as of version 1.3 by default looks for classes in four places in order, local JCL class loader (order=1), 
  current class loader (order=2), parent class loader (order=3) and then in system class loader (order=4). More 
  class loaders can be added by extending the <i>xeus.jcl.loader.Loader</i> class. 
  Each loader has a loading order, the default loading orders can be changed. Below 
  is an example that shows how this loading order can be changed. </p>
  <pre name="code" class="java">JarClassLoader jcl=new JarClassLoader();
jcl.add(&quot;myjar.jar&quot;); // Add some class source

jcl.getSystemLoader().setOrder(1); // Look for classes in system class loader first
jcl.getLocalLoader().setOrder(2); // if not found look for classes in local class loader
jcl.getParentLoader().setOrder(3); // if not found look for classes in parent class loader
jcl.getCurrentLoader().setOrder(4); // if not found look for classes in current class loader

// A custom class loader that extends xeus.jcl.loader.Loader
MyLoader loader=new MyLoader();
loader.setOrder(5);

jcl.addLoader(loader); //Add custom loader</pre>
<h1>Resources</h1>
  <p>

	- A nice <a href="http://thinkinginsoftware.blogspot.com/2009/01/using-java-class-loader-jcl-to-reload.html">tutorial with examples</a> by Nestor<br/>
	- Javadoc <a href="http://jcloader.sourceforge.net/api/">API</a> for more details.
	</p>
  <h1>Downloads</h1>
  <p>JCL is open source under <a href="http://www.gnu.org/copyleft/gpl.html">GPL</a> 
  and is available for <a href="http://www.sourceforge.net/projects/jcloader">download</a> 
  on sourceforge. The source can be anonymously checked-out from subversion using 
  the following command:<br />
  <br />
  svn co https://jcloader.svn.sourceforge.net/svnroot/jcloader/trunk/JCL JCL</p>
  <br />
  </div>
  <div id="footer">
    <p>Copyright &copy; <a href="http://xeustech.blogspot.com/">Xeus Technologies</a> 
    2009 | Valid <a href="http://jigsaw.w3.org/css-validator/">CSS</a> &amp;
    <a href="http://validator.w3.org/">XHTML</a></p>
  </div>

</body>

</html>


